using System;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// 
    /// - The <see cref="IBOSelectorAndEditor"/> for <see cref="DMPropRuleParameter"/>s 
    /// - The Min Max etc values for the different rule types.
    /// </summary>
    public class DMPropRuleParameterControl : UserControlWin, IFormControl, IBusinessObjectControl
    {
        private DMPropRule _propertyRule;
        private readonly IEditableGridControl _selectorControl;

        public DMPropRuleParameterControl(IControlFactory controlFactory)
        {
            if (controlFactory == null) throw new ArgumentNullException("controlFactory");
            
            _selectorControl = controlFactory.CreateEditableGridControl();
            _selectorControl.Grid.AllowUserToAddRows = false;
            _selectorControl.Grid.AllowUserToDeleteRows = false;
            _selectorControl.Buttons.Visible = false;
            BorderLayoutManager layoutManager = controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(_selectorControl, BorderLayoutManager.Position.Centre);
            this.Enabled = false;
            _selectorControl.AutoSelectFirstItem = false;
        }

        /// <summary>
        /// The <see cref="DMPropRule"/> whose <see cref="DMPropRuleParameter"/>s are being edited by this control
        /// </summary>
        public DMPropRule PropertyRule
        {
            get { return _propertyRule; }
            set
            {
                _propertyRule = value;
                if (_propertyRule == null)
                {

                    SelectorControl.BusinessObjectCollection = new BusinessObjectCollection<DMPropRuleParameter>();
                    _selectorControl.Grid.AllowUserToAddRows = false;
                    this.Enabled = false;
                    return;
                }
                this.Enabled = true;
                BusinessObjectCollection<DMPropRuleParameter> col = _propertyRule.PropRuleParameters;
                SelectorControl.BusinessObjectCollection = col;
                _selectorControl.Grid.AllowUserToAddRows = false;
            }
        }
        /// <summary>
        /// The <see cref="IBusinessObjectCollection"/> of <see cref="DMPropRuleParameter"/>s being managed by this control
        /// </summary>
        public BusinessObjectCollection<DMPropRuleParameter> PropRuleParameters
        {
            get { return (BusinessObjectCollection<DMPropRuleParameter>)SelectorControl.BusinessObjectCollection; }
        }

        public void SetForm(IFormHabanero form)
        {}

        ///<summary>
        /// Gets or sets the business object being represented In this case the <see cref="DMPropRule"/>
        ///</summary>
        public IBusinessObject BusinessObject
        {
            get { return this.PropertyRule; }
            set { this.PropertyRule = (DMPropRule) value; }
        }

        public IBOColSelectorControl SelectorControl
        {
            get { return _selectorControl; }
        }
    }
}
